'use strict';
const Controller = require('egg').Controller;

class GoodsController extends Controller {
  /**
       * @api {Post} /api/goods 添加商品
       * @apiGroup 商品
       * @apiParam {String}  name             商品名称    （必填）
       * @apiParam {String}  desc             商品副标题描述
       * @apiParam {INTEGER} classify_id      分类id      （必填）
       * @apiParam {String}  price            价格        （必填）
       * @apiParam {String}  photo_ids        图片组id，  (多个id时，用英文逗号拼接)
       * @apiParam {TEXT}  rich_text          商品详情(富文本内容)
       * @apiParam {INTEGER} stock_num        库存
       * @apiParamExample 参数模板
       {
        "name": "电饭煲",
        "classify_id": 6,
        "price": 100,
        "photo_ids": "1,2,3",
        "stock_num": 99
        }
       * @apiSuccessExample 成功返回
       * {
       *    "msg":'添加成功'
       * }
       *
       * @apiErrorExample 错误返回
       * {
       *  "msg":"该商品名已存在"
       * }
       * @apiErrorExample 错误返回
       * {
       *  "msg":"添加失败"
       * }
       *
       */

  async create() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;

    if (
      new ctx.helper.checkData(
        this,
        'name',
        'classify_id',
        'price',
        'photo_ids',
        'stock_num'
      ).checkDataRes
    ) {
      ctx.body = await service.shop.goods.create(dataObj);
    }
  }

  // 获取商品列表
  /**
       * @api {Get} /api/goods 获取商品列表
       * @apiGroup 商品
       * @apiParam {String}  page             第几页
       * @apiParam {String}  limit            每页多少条数据
       * @apiParam {INTEGER} project_id      项目id(不传时默认使用后台设置的项目id)
       * @apiParam {String}  classify_id      分类id（筛选用）
       * @apiParam {String}  name             商品名称（搜索用）
       * @apiParamExample 参数模板
       {
           page:1,
           limit: 10
       }
       * @apiSuccessExample  成功返回
       {
              "msg": "成功操作",
              "result": {
                  "count": 11,
                  "rows": [
                      {
                          "id": 11,
                          "name": "gashjhll",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "5",
                          "price": 43434,
                          "photo_ids": "16",
                          "stock_num": 344334,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:03:01.000Z",
                          "updatedAt": "2020-08-25T09:03:01.000Z",
                          "s_photos": [
                              {
                                  "id": 16,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346180491-7da66c1da744f307dabadd25235c02b5.webp"
                              }
                          ]
                      },
                      {
                          "id": 10,
                          "name": "gasdg",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "9",
                          "price": 23,
                          "photo_ids": "15",
                          "stock_num": 32,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:02:46.000Z",
                          "updatedAt": "2020-08-25T09:02:46.000Z",
                          "s_photos": [
                              {
                                  "id": 15,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346165753-1b235db1996e0d88a96417f72dd65d6e.webp"
                              }
                          ]
                      },
                      {
                          "id": 9,
                          "name": "adgagGag噶时光",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "8",
                          "price": 3423,
                          "photo_ids": "14",
                          "stock_num": 234,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:02:37.000Z",
                          "updatedAt": "2020-08-25T09:02:37.000Z",
                          "s_photos": [
                              {
                                  "id": 14,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346155947-50708184a191b1f88df061bdd3123e6c.webp"
                              }
                          ]
                      },
                      {
                          "id": 8,
                          "name": "asg尬的归属感",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "8",
                          "price": 3434,
                          "photo_ids": "13",
                          "stock_num": 4343,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:02:21.000Z",
                          "updatedAt": "2020-08-25T09:02:21.000Z",
                          "s_photos": [
                              {
                                  "id": 13,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346140773-8497ad64857579f39281d9de0a1d7ab1.webp"
                              }
                          ]
                      },
                      {
                          "id": 7,
                          "name": "etqt",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "3",
                          "price": 3434,
                          "photo_ids": "12",
                          "stock_num": 3434,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:02:10.000Z",
                          "updatedAt": "2020-08-25T09:02:10.000Z",
                          "s_photos": [
                              {
                                  "id": 12,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346129328-c955d087acae8e8947f5bacc324d59df.webp"
                              }
                          ]
                      },
                      {
                          "id": 6,
                          "name": "3asgasg",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "5",
                          "price": 2324,
                          "photo_ids": "11",
                          "stock_num": 343,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:02:01.000Z",
                          "updatedAt": "2020-08-25T09:02:01.000Z",
                          "s_photos": [
                              {
                                  "id": 11,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346119770-8aa72c2650dbd0f416cd2ca7ddd40c5d.jpg"
                              }
                          ]
                      },
                      {
                          "id": 5,
                          "name": "撒大噶山豆根",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "3",
                          "price": 23234,
                          "photo_ids": "10",
                          "stock_num": 3434,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:01:52.000Z",
                          "updatedAt": "2020-08-25T09:01:52.000Z",
                          "s_photos": [
                              {
                                  "id": 10,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346109633-2bc3f021bda19a41d38ff747cf97e271.webp"
                              }
                          ]
                      },
                      {
                          "id": 4,
                          "name": "ddsagsg",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "3",
                          "price": 323,
                          "photo_ids": "9",
                          "stock_num": 4,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T09:01:40.000Z",
                          "updatedAt": "2020-08-25T09:01:40.000Z",
                          "s_photos": [
                              {
                                  "id": 9,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598346098179-ee935a381d7f64f85883be6acd340eda.webp"
                              }
                          ]
                      },
                      {
                          "id": 3,
                          "name": "电饭煲",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "3",
                          "price": 299,
                          "photo_ids": "3",
                          "stock_num": 100,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T08:07:16.000Z",
                          "updatedAt": "2020-08-25T08:07:16.000Z",
                          "s_photos": [
                              {
                                  "id": 3,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598342834486-efc7da8df082905ed77570509e96f33c.jpg"
                              }
                          ]
                      },
                      {
                          "id": 2,
                          "name": "小米",
                          "desc": "",
                          "shops_id": 1,
                          "classify_id": "9",
                          "price": 6999,
                          "photo_ids": "8",
                          "stock_num": 100,
                          "sold_num": 0,
                          "status": "1",
                          "rich_text": null,
                          "createdAt": "2020-08-25T08:06:53.000Z",
                          "updatedAt": "2020-08-25T08:41:27.000Z",
                          "s_photos": [
                              {
                                  "id": 8,
                                  "path": "http://127.0.0.1:7002/public/uploads/1598344886461-d6b57d87409f1ccbde695d05e2c47e78.webp"
                              }
                          ]
                      }
                  ]
              }
          }
       * @apiErrorExample  错误返回
       * {
       *   "msg": "请登录后再进行操作"
       * }
       */

  async index() {
    const { ctx, service } = this;

    ctx.body = await service.shop.goods.index();
  }

  // 获取热门商品
  /**
       * @api {Get} /api/goods/hotGoods 获取热门商品(根据已销售数量)
       * @apiGroup 商品
       * @apiParam {INTEGER} project_id       项目id(不传时默认使用后台设置的项目id)
       * @apiParamExample 参数模板
       {}
       * @apiSuccessExample  成功返回
       {}
       * @apiErrorExample  错误返回
       * {
       *   "msg": "请登录后再进行操作"
       * }
       */

  async hotGoods() {
    const { ctx, service } = this;
    ctx.body = await service.shop.goods.hotGoods();
  }

  //搜索商品
  /**
   * @api {get} /api/goodsSearch 搜索商品
   * @apiGroup 商品
       * @apiParam {String}  name             搜索关键词
       * @apiParam {String}  project_id       项目ID
       * @apiParamExample 参数模板
       {
           name:'衣服',
           project_id: 207
       }
   * @apiSuccessExample 成功返回
   * {
   *    "msg": "成功操作",
    "result": {
        "count": 1,
        "rows": [
            {
                "id": 6,
                "name": "这是最后的测试商品",
                "desc": "this is the finally test goods",
                "shops_id": 1,
                "project_id": 1,
                "classify_id": 9,
                "price": 123,
                "sale_price": 12,
                "photo_ids": "10,11",
                "stock_num": 123,
                "sold_num": 0,
                "search_num": 1,
                "status": "1",
                "rich_text": "",
                "classify_ids": "9",
                "createdAt": "2021-08-13T03:13:59.000Z",
                "updatedAt": "2021-08-13T03:41:54.638Z",
                "s_goods_photos": [
                    {
                        "id": 10,
                        "path": "http://shops-1251395798.cos.ap-nanjing.myqcloud.com/1628824426052-d679146774edad7cadd467be0b9c5c2b.jpg"
                    },
                    {
                        "id": 11,
                        "path": "http://shops-1251395798.cos.ap-nanjing.myqcloud.com/1628824426010-8e742071192795b9fdca4b2470fa46aa.jpg"
                    }
                ]
            }
        ]
    }
   * }
   *
   * @apiErrorExample 错误返回
   * {
   *  "msg":"查询商品信息失败"
   * }
   *
   */
  async search() {
    const { ctx, service } = this;
    ctx.body = await service.shop.goods.search();
  }
  //根据分类id获取商品信息
  /**
         * @api {Get} /api/goods/getById/:id 根据分类id获取商品信息
         * @apiGroup 商品
         * @apiParamExample 参数模板
         {}
         * @apiSuccessExample  成功返回
            {
            "msg": "成功操作",
            "result": {
                "count": 2,
                "rows": [
                    {
                        "id": 11,
                        "name": "goods_iphone",
                        "desc": "this is a smart phone producted by apple",
                        "shops_id": 1,
                        "classify_id": 2,
                        "price": 43434,
                        "photo_ids": "16",
                        "stock_num": 344334,
                        "sold_num": 0,
                        "status": "1",
                        "rich_text": null,
                        "createdAt": "2020-08-25T09:03:01.000Z",
                        "updatedAt": "2020-08-25T09:03:01.000Z",
                        "s_photos": [
                            {
                                "id": 16,
                                "path": "http://127.0.0.1:7002/public/uploads/1598346180491-7da66c1da744f307dabadd25235c02b5.webp"
                            }
                        ]
                    },
                    {
                        "id": 10,
                        "name": "gasdg",
                        "desc": null,
                        "shops_id": 1,
                        "classify_id": 2,
                        "price": 23,
                        "photo_ids": "15",
                        "stock_num": 32,
                        "sold_num": 0,
                        "status": "1",
                        "rich_text": null,
                        "createdAt": "2020-08-25T09:02:46.000Z",
                        "updatedAt": "2020-08-25T09:02:46.000Z",
                        "s_photos": [
                            {
                                "id": 15,
                                "path": "http://127.0.0.1:7002/public/uploads/1598346165753-1b235db1996e0d88a96417f72dd65d6e.webp"
                            }
                        ]
                    }
                ]
            }
        }
         * @apiErrorExample  错误返回
         * {
         *   "msg": "请登录后再进行操作"
         * }
         */
  async getById() {
    const { ctx, service } = this;
    const id = ctx.params.id;

    ctx.body = await service.shop.goods.getByClassifyId(id);
  }
  //获取某类商品的销售数量
  /**
         * @api {Get} /api/goods/getSoldNum/:id 获取某类商品的销售数量
         * @apiGroup 商品
         * @apiParamExample 参数模板
         {}
         * @apiSuccessExample  成功返回
            {
            "msg": "成功操作",
                "result": {
                "sold_total": 4,总销售数量
                "classify_id": "1"，分类id
                }
        }
         * @apiErrorExample  错误返回
         * {
         *   "msg": "找不到该类商品"
         * }
         */
  async getSoldNum() {
    const { ctx, service } = this;
    ctx.body = await service.shop.goods.getSoldNum();
  }

  async getAllNum() {
    const { ctx, service } = this;
    ctx.body = await service.shop.goods.getAllNum();
  }
  // 获取商品单个信息
  /**
       * @api {Get} /api/goods/:id 获取商品单个信息
       * @apiGroup 商品
       * @apiParamExample 参数模板
       {}
       * @apiSuccessExample  成功返回
        {
              "msg": "成功操作",
              "result": {
                  "id": 11,
                  "name": "gashjhll",
                  "desc": "",
                  "shops_id": 1,
                  "classify_id": "5",
                  "price": 43434,
                  "photo_ids": "16",
                  "stock_num": 344334,
                  "sold_num": 0,
                  "status": "1",
                  "rich_text": null,
                  "createdAt": "2020-08-25T09:03:01.000Z",
                  "updatedAt": "2020-08-25T09:03:01.000Z",
                  "s_photos": [
                      {
                          "id": 16,
                          "path": "http://127.0.0.1:7002/public/uploads/1598346180491-7da66c1da744f307dabadd25235c02b5.webp"
                      }
                  ]
              }
          }
       * @apiErrorExample  错误返回
       * {
       *   "msg": "请登录后再进行操作"
       * }
       */

  async show() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    ctx.body = await service.shop.goods.show(id);
  }
  // 更新商品信息
  /**
         * @api {update} /api/goods/:id 更新商品信息
         * @apiGroup 商品
         * @apiParam {String}  name             商品名称    （必填）
         * @apiParam {String}  desc             商品描述
         * @apiParam {INTEGER} classify_id      分类id      （必填）
         * @apiParam {String}  price            价格    （必填）
         * @apiParam {String}  photo_ids        图片组id，  (多个id时，用英文逗号拼接)
         * @apiParam {INTEGER} stock_num        库存
         * @apiParam {String}  rich_text        商品详情
         * @apiParam {String}  status           状态，0停用,1可用,2删除 (默认1)
         * @apiParamExample 参数模板
         {
          "name": "电饭煲",
          "classify_id": 6,
          "price": 100,
          "photo_ids": "1,2,3",
          "stock_num": 99
          }
         * @apiSuccessExample  成功返回
         {
             msg: '更新成功'
         }
         * @apiErrorExample  错误返回
         * {
         *   "msg": "请登录后再进行操作"
         * }
         */

  async update() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;

    if (
      new ctx.helper.checkData(
        this,
        'name',
        'classify_id',
        'price',
        'photo_ids',
        'stock_num'
      ).checkDataRes
    ) {
      const id = ctx.params.id;
      ctx.body = await service.shop.goods.update(id, dataObj);
    }
  }
  /**
       * @api {delete} /api/goods/:id 删除商品
       * @apiGroup 商品
       * @apiParamExample 参数模板
       {}
       * @apiSuccessExample 成功返回
       * {
       *    "msg":'删除成功'
       * }
       *
       * @apiErrorExample 错误返回
       * {
       *  "msg":"删除失败"
       * }
       *
       */

  async destroy() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    ctx.body = await service.shop.goods.destroy(id);
  }
}

module.exports = GoodsController;
